<template>
  <div class="gk-layout-container">
    <div class="gk-layout-header">
      <s-query perms="material:classifyarea" @click="findPage" add=true @addClick="handleAdd">
        <el-form-item>
          <el-select v-model="filters.classifyId" placeholder="请选择物料类别" style="width: 100%;" clearable>
            <el-option v-for="item in classTypes" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select v-model="filters.areaId" placeholder="请选择区域" auto-complete="off" style="width: 100%;" clearable>
            <el-option v-for="item in areaTypes" :key="item.id" :label="item.areaName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

      </s-query>
      <s-reflash reflash="true" @findPage="findPage"> </s-reflash>
    </div>




    <div class="gk-layout-main">
      <my-table :data="pageResult.content" :loading="loading" @selection-change="selectionChange">
        <el-table-column :label="$t('action.desc')" width="50" header-align="center" align="center">
          <template slot-scope="scope"> {{ scope.$index + 1 }} </template>
        </el-table-column>
        <el-table-column type="selection" width="40"></el-table-column>
        <el-table-column header-align="center" align="center" prop="classifyId" label="类别名称" key="classifyId"
          :formatter="selectionFormats"> </el-table-column>
        <el-table-column header-align="center" align="center" prop="areaId" label="区域名称" key="areaId"
          :formatter="selectionFormat"> </el-table-column>
        <el-table-column header-align="center" align="center" prop="eigen" label="特征" key="eigen"
          :formatter="eigenFilter"> </el-table-column>
 
        <el-table-column header-align="center" align="center" :label="$t('action.operation')">
          <template slot-scope="scope">
            <kt-delete-button :label="$t('action.delete')" perms="material:classifyarea:delete"
              @click="handleDelete(scope.$index, scope.row)" />
            <kt-button :label="$t('action.edit')" perms="material:classifyarea:edit"
              @click="handleEdit(scope.$index, scope.row)" />
          </template>
        </el-table-column>
      </my-table>
    </div>
    <!--分页栏-->
    <div class="toolbar gk-layout-footer" style="padding:10px;">
      <kt-delete-button perms="material:classifyarea:delete" @click="handleBatchDelete()" style="float:left;" />
      <el-pagination layout="total, prev, pager, next, jumper" @current-change="findPage" :current-page="filters.pageNum"
        :page-size="filters.pageSize" :total="pageResult.totalSize" style="float:right;">
      </el-pagination>
    </div>



    <!--新增编辑界面-->
    <el-dialog :title="operation ? '物料类别分区 —— 新增' : '物料类别分区 —— 编辑'" width="40%" :visible.sync="dialogVisible"
      :close-on-click-modal="false">
      <el-form :model="dataForm" label-width="90px" :rules="dataFormRules" ref="dataForm" :size="global.gSize"
        label-position="right">
        <el-form-item label="ID" prop="id" v-if="false">
          <el-input v-model="dataForm.id" :disabled="true" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item :label="$t('item.classifyId')" prop="classifyId">
          <el-select v-model="dataForm.classifyId" placeholder="请选择物料类别" style="width: 100%;" clearable filterable>
            <el-option v-for="item in classTypes" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('field.Core.corewhloc.areaName')" prop="areaId">
          <el-select v-model="dataForm.areaId" placeholder="请选择区域" auto-complete="off" style="width: 100%;" clearable
            filterable>
            <el-option v-for="item in areaTypes" :key="item.id" :label="item.areaName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

 
        <s-dict-form name="eigen" v-model="dataForm.eigen"> </s-dict-form>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :size="global.gSize" @click.native="dialogVisible = false">{{ $t('action.cancel') }}</el-button>
        <el-button :size="global.gSize" type="primary" @click.native="submitForm" :loading="editLoading">{{
          $t('action.submit') }}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import PopupTreeInput from "@/components/PopupTreeInput"
import MyTable from "@/views/Core/MyTable"
import KtButton from "@/views/Core/KtButton"
import KtDeleteButton from "@/views/Core/KtDeleteButton"
import SQuery from "@/components/Kj/SQuery"
import SReflash from "@/components/Kj/SReflash"
import SDictForm from "@/components/Kj/SDictForm"
import { format } from "@/utils/datetime"
import { dictsFormat, getItemName } from "@/utils/myUtil"

export default {
  components: {
    PopupTreeInput,
    MyTable,
    KtButton,
    KtDeleteButton,
    SReflash, SQuery, SDictForm
  },
  data() {
    return {
      size: 'small',
      filters: {
        name: '', pageNum: 1, pageSize: this.global.pageSize
      },
      loading: false,
      demo: [],
      pageResult: {},
      operation: false, // true:新增, false:编辑
      dialogVisible: false,
      dialogVisibles: false,// 设置区位界面是否显示
      editLoading: false,
      selections: [],
      // 新增编辑界面数据
      dataForm: {
        id: 0,
        classifyId: '',
        areaId: '',
        priority: '3',
        eigen: 0
      },
      areaTypes: [],
      classTypes: [],
      eigenTypes: [],
      priorityTypes: []
    }
  },
  computed: {
    dataFormRules() {
      const dataFormRules = {
        classifyId: [{ required: true, message: "请选择物料类别", trigger: 'blur' }],
        areaId: [{ required: true, message: "请选择区域名称", trigger: 'blur' }]
      };
      return dataFormRules;
    }
  },
  methods: {
    // 获取分页数据
    findPage: function (data) {

      if (data != -1) {
        this.filters.pageNum = data
      }

      this.$api.classifyarea.findPage(this.filters).then((res) => {
        this.pageResult = res.data
      })
    },

    findAreaTypes: function () {
      this.$api.corewharea.findPage().then((res) => {
        this.areaTypes = res.data.content
      })
    },
    //加载物料类别分类
    findClassTypes: function () {
      this.$api.classify.findPage().then((res) => {
        this.classTypes = res.data.content
      })
    },
    findEigenTypes: function () {
      this.$api.classifyarea.findPage().then((res) => {
        this.eigenTypes = res.data.content
      })
    },
    findPriorityTypes: function () {
      this.$api.classifyarea.findPage().then((res) => {
        this.priorityTypes = res.data.content
      })
    },
    //批量编辑
    submitForms: function () {

      //let para = this.handleBatchEdit(data).data
      let ppa = [];
      for (var i = 0; i < this.demo.selections.length; i++) {

        ppa.push(this.demo.selections[i].id)
      }

      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.$confirm(this.getKey('action.isConfirm'), this.getKey('action.tips'), {}).then(() => {
            this.editLoading = true
            let params = this.dataForm.areaId + "/" + this.dataForm.eigen
            this.$api.classifyarea.batchset(ppa, params).then((res) => {
              this.editLoading = false
              if (res.code == 200) {
                this.$message({ message: this.getKey('action.operateSucess'), type: 'success' })
                this.dialogVisibles = false
                this.$refs['dataForm'].resetFields()
              } else {
                this.$message({ message: this.getKey('action.operateFail') + res.msg, type: 'error' })
              }
              this.findPage(-1)
            })
          })
        }
      })
    },
    selectionChange: function (selection) {

      this.selection = selection;
    },
    //显示批量设置库位界面
    handleBatchSet: function () {
      this.dialogVisibles = true
      this.operation = true
      this.demo = this.selection;
      console.log(this.demo);
    },

    selectionChange: function (selection) {
      this.selections = selection;
    },
    handleBatchDelete: function () {
      let ids = this.selections.map(item => item.id).toString()
      let params = []
      let idArray = (ids + '').split(',')
      for (var i = 0; i < idArray.length; i++) {
        params.push({ 'id': idArray[i] })
      }
      this.$api.classifyarea.batchDelete(params).then((res) => {
        if (res.code == "500") {

          this.$message({ message: this.getKey('action.operateFail') + res.msg, type: 'error' })
        }
        this.findPage(-1)
      });
    },
    // 批量删除
    handleDelete: function (index, data) {
      let params = [{ 'id': data.id }]

      this.$api.classifyarea.batchDelete(params).then((res) => {
        if (res.code == "500") {

          this.$message({ message: this.getKey('action.operateFail') + res.msg, type: 'error' })
        }
        this.findPage(-1)
      });

    },

    // 显示新增界面
    handleAdd: function () {
      this.dialogVisible = true
      this.operation = true
      this.dataForm = {
        id: 0,
        classifyId: '',
        areaId: '',
        priority: 1,
        eigen: 0
      }
    },
    // 显示编辑界面
    handleEdit: function (params, row) {
      this.dialogVisible = true
      this.operation = false
      this.dataForm = Object.assign({}, row)
    },
    // 编辑
    submitForm: function () {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.$confirm(this.getKey('action.isConfirm'), this.getKey('action.tips'), {}).then(() => {
            this.editLoading = true
            let params = Object.assign({}, this.dataForm)
            this.$api.classifyarea.save(params).then((res) => {
              this.editLoading = false
              if (res.code == 200) {
                this.$message({ message: this.getKey('action.operateSucess'), type: 'success' })
                this.dialogVisible = false
                this.$refs['dataForm'].resetFields()
              } else {
                this.$message({ message: this.getKey('action.operateFail') + res.msg, type: 'error' })
              }
              this.findPage(-1)
            })
          })
        }
      })
    },



    getKey: function (arg) {
      return this.$t(arg)
    },

    selectionFormat: function (row, column, cellValue, index) {
      let key = ""
      let propt = column.property;
      let val = row[column.property];
      let dict = this.areaTypes;
      for (let i = 0; i < dict.length; i++) {
        if (dict[i].id == val) {
          return dict[i].areaName;
        }
      }
      return row[column.property]
    },
    itemFilter: function (row, column, val, index) {
      return dictsFormat("classifyAreaPriority", val)
    },
    eigenFilter: function (row, column, val, index) {
      return dictsFormat("eigen", val)
    },
    selectionFormats: function (row, column, cellValue, index) {
      let key = ""
      let propt = column.property;
      let val = row[column.property];
      let dict = this.classTypes;
      for (let i = 0; i < dict.length; i++) {
        if (dict[i].id == val) {
          return dict[i].name;
        }
      }
      return row[column.property]
    },
  },
  created() {
    this.findAreaTypes();
    this.findClassTypes();
    this.findEigenTypes();
    this.findPriorityTypes();
    this.findPage(1);

  }
}
</script>

<style scoped></style>